package com.matt.repository.record;

import com.matt.domain.record.CheckRecord;
import com.matt.foundation.vo.ChildrenCheckRecordVo;
import com.matt.repository.base.BaseNativeSqlRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CheckRecordNativeRepository extends BaseNativeSqlRepository {


    /**
     * @param month   例如 "2019/05"
     */
    public List<ChildrenCheckRecordVo> findMonthRecordByChildrenId(@Param("childrenId") String childrenId, String month){
        String sql = "select childrenId,imgUrl,gender,cr.status as checkStatus,checkInTime,checkOutTime,healthyStatus,sickName,month, " +
                "c.name as childrenName from check_record cr left join children c on cr.childrenId=c.id " +
                "where childrenId='%s' and checkInTime>='%s/1 00:00' and checkInTime<DATE_ADD('%s/1 00:00', INTERVAL 1 MONTH)";

        sql =  String.format(sql, childrenId, month, month);

        List<ChildrenCheckRecordVo> items = sqlObjectList(sql, ChildrenCheckRecordVo.class);

        return items;
    }

    /**
     * @param day   例如 "2019/05/10"
     */
    public List<CheckRecord> findDayRecordByChildrenId(@Param("childrenId") String childrenId, String day){
        String sql = "select * from check_record cr " +
                "where childrenId='%s' and checkInTime>='%s 00:00' and checkInTime<DATE_ADD('%s 00:00', INTERVAL 1 DAY)";

        sql =  String.format(sql, childrenId, day, day);

        List<CheckRecord> items = sqlObjectList(sql, CheckRecord.class);

        return items;
    }

    public CheckRecord findRecordById(String checkRecordId){
        String sql = "select * from check_record cr where id='%s'";

        sql =  String.format(sql, checkRecordId);

        List<CheckRecord> items = sqlObjectList(sql, CheckRecord.class);

        return items.size()>0 ? items.get(0) : null;
    }
}
